﻿using mozhi.smarterp.Finance.Basedatas.Events;
using mozhi.smarterp.Finance.Enums;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using Volo.Abp;

namespace mozhi.smarterp.Finance.Basedatas
{
    /// <summary>
    /// 核算维度组合
    /// </summary>
    public class DimensionGroupValue : AggregateRootBase<Guid>
    {
        /// <summary>
        /// 维度组合的hash值
        /// </summary>
        public string UniqueValueHash { private set; get; }

        public string UniqueValue { private set; get; }

        /// <summary>
        /// 明细项
        /// </summary>
        public List<DimensionGroupValueItem> Items { private set; get; }

        public DimensionGroupValue(Guid id)
        {
            Id = id;
            Items = new List<DimensionGroupValueItem>();
        }

        public void ChangeItemValue(Guid itemId, string itemValue)
        {
            var match = Items.FirstOrDefault(x => x.Id == itemId);

            if (match == null)
                throw new BusinessException($"核算维度值项:{itemId}不存在");

            var oldValue = match.Value;

            match.ChangeValue(itemValue);

            if (!oldValue.IsNullOrEmpty() && oldValue != itemValue)
            {
                Guid occupiedId = default;

                if (Guid.TryParse(match.Value, out occupiedId))
                    AddDistributedEvent(new DataAssociationRemoveEvent(Id, occupiedId));
            }

            if (!match.Value.IsNullOrEmpty())
            {
                Guid occupiedId = default;

                if (Guid.TryParse(match.Value, out occupiedId))
                    AddDistributedEvent(new DataAssociationAddEvent(Id, nameof(DimensionGroupValue), "核算维度值", match.Label, "", occupiedId, Enum.GetName(typeof(DataSourceType), match.DataSourceType)));
            }


            UniqueValue = GetUniqueValue();
        }

        public string GetUniqueValue()
        { 

            if (Items.Count == 0)
                return "[]";

            List<KeyValuePair<string, string>> dit = new List<KeyValuePair<string, string>>();

            foreach(var item in Items)
            {
                if (item.DataSourceType != DataSourceType.Tag)
                    dit.Add(new KeyValuePair<string, string>(item.DataSourceType.ToString(), item.Value));
                else
                    dit.Add(new KeyValuePair<string, string>(item.TagId.ToString().ToLower(), item.Value));
            }

            //排序
            dit = dit.OrderBy(x=>x.Key).ToList();

            return JsonConvert.SerializeObject(dit);
        }

        public void ChangeUniqueValue()
        {
            this.UniqueValue = GetUniqueValue();
        }
    }

    /// <summary>
    /// 核算维度组合项
    /// </summary>
    public class DimensionGroupValueItem : EntityBase<Guid>
    {
        /// <summary>
        /// 数据来源
        /// </summary>
        public DataSourceType DataSourceType { private set; get; }

        /// <summary>
        /// 标签
        /// </summary>
        public Guid? TagId { private set; get; }


        public string Value { private set; get; }

        /// <summary>
        /// 值的显示名称
        /// </summary>
        public string Label { set; get; }

        public Guid DimensionGroupValueId { set; get;}

        public DimensionGroupValueItem(Guid id , Guid dimensionGroupValueId, DataSourceType dataSourceType, Guid? tagId, string value)
        {
            Id = id;
            DataSourceType = dataSourceType;
            TagId = tagId;
            Value = value?.ToLower();
            DimensionGroupValueId = dimensionGroupValueId;

            //如果是标签核算维度，必须要又标签值
            if (dataSourceType == DataSourceType.Tag && !tagId.HasValue)
                throw new UserFriendlyException("数据来源为标签的核算维度，必须传入具体的标签Id");

           
        }

        public void ChangeValue(string value)
        {
            this.Value = value?.ToLower();
        }
    }
}
